
global name "C0_cash_prepare_additionalvars"
cap log close
log using "${pfile}/${name}.smcl", replace smcl

*Parameteres: YEAR   KINK  SLOPE BEN K-N MI MF   YF DF INF \ YEAR   KINK   SLOPE  BEN   K-N MI MF   YF  DF INF \ YEAR   KINK   SLOPE   BEN   K-N MI MF  YF  DF INF \
local y_2001 "2001, 225.00, 0.8, 180, 1, 4, 3, 2002, 31, 2.021 \ 2001, 297.14, -0.3, 235.31, 2, 4, 3, 2002, 31, 2.021 \ 2001, 495.28, -0.5, 335.88, 3, 4, 3, 2002, 31, 2.021"
local y_2002 "2002, 250.00, 0.8, 200, 1, 4, 3, 2003, 31, 1.877 \ 2002, 330.14, -0.3, 264.11, 2, 4, 3, 2003, 31, 1.877 \ 2002, 550.31, -0.5, 374.19, 3, 4, 3, 2003, 31, 1.877" 
local y_2003 "2003, 300.00, 0.8, 240, 1, 4, 3, 2004, 31, 1.668 \ 2003, 396.18, -0.3, 316.94, 2, 4, 3, 2004, 31, 1.668 \ 2003, 660.37, -0.5, 449.03, 3, 4, 3, 2004, 31, 1.668"
local y_2004 "2004, 325.00, 0.8, 260, 1, 4, 4, 2005, 31, 1.526 \ 2004, 429.20, -0.3, 343.36, 2, 4, 4, 2005, 31, 1.526 \ 2004, 715.40, -0.5, 486.46, 3, 4, 4, 2005, 31, 1.526"
local y_2005 "2005, 375.00, 0.8, 300, 1, 5, 3, 2006, 31, 1.419 \ 2005, 495.23, -0.3, 396.18, 2, 5, 3, 2006, 31, 1.419 \ 2005, 825.46, -0.5, 561.30, 3, 5, 3, 2006, 31, 1.419"
local y_2006 "2006, 437.50, 0.8, 350, 1, 4, 3, 2007, 31, 1.342 \ 2006, 577.77, -0.3, 462.22, 2, 4, 3, 2007, 31, 1.342 \ 2006, 963.04, -0.5, 654.85, 3, 4, 3, 2007, 31, 1.342"
local y_2007 "2007, 475.00, 0.8, 380, 1, 4, 2, 2008, 28, 1.302 \ 2007, 627.29, -0.3, 501.83, 2, 4, 2, 2008, 28, 1.302\ 2007, 1045.48, -0.5, 710.93, 3, 4, 2, 2008, 28, 1.302"
local y_2008 "2008, 518.75, 0.8, 415, 1, 3, 1, 2009, 31, 1.246 \ 2008, 685.06, -0.3, 548.05, 2, 3, 1, 2009, 31, 1.246 \ 2008, 1141.88, -0.5, 776.46, 3, 3, 1, 2009, 31, 1.246"
local y_2009 "2009, 581.25, 0.8, 465, 1, 2, 12, 2009, 31, 1.177 \2009, 767.60, -0.3, 614.08, 2, 2, 12, 2009, 31, 1.177 \ 2009, 1279.46, -0.5, 870.01, 3, 2, 12, 2009, 31, 1.177"
local y_2010 "2010, 637.50, 0.8, 510, 1, 1, 12, 2010, 31, 1.128 \ 2010, 841.88, -0.3, 673.50, 2, 1, 12, 2010, 31, 1.128 \ 2010, 1403.28, -0.5, 954.20, 3, 1, 12, 2010, 31, 1.128"
local y_2011 "2011, 675.00, 0.8, 540, 1, 1, 12, 2011, 31, 1.065 \ 2011, 891.40, -0.3, 713.12, 2, 1, 12, 2011, 31, 1.065 \ 2011, 1485.83, -0.5, 1010.35, 3, 1, 12, 2011, 31, 1.065"
local y_2012 "2012, 777.50, 0.8, 622, 1, 1, 12, 2012, 31, 1    \ 2012, 1026.77, -0.3, 821.41, 2, 1, 12, 2012, 31, 1    \ 2012, 1711.45, -0.5, 1163.75, 3, 1, 12, 2012, 31, 1   "
local y_2013 "2013, 847.50, 0.8, 678, 1, 1, 12, 2013, 31, 0.944  \ 2013, 1090.43, -0.3, 872.34, 2, 1, 12, 2013, 31, 0.944  \ 2013, 1817.56, -0.5, 1235.91, 3, 1, 12, 2013, 31, 0.944"
local y_2014 "2014, 905.00, 0.8, 724, 1, 1, 12, 2014, 31, 0.887  \ 2014, 1151.06, -0.3, 920.84, 2, 1, 12, 2014, 31, 0.887    \ 2014, 1918.62, -0.5, 1304.63, 3, 1, 12, 2014, 31, 0.887"
local y_2015 "2015, 985.00, 0.8, 788, 1, 1, 12, 2015, 31, 0.801  \ 2015, 1222.77, -0.3, 978.26, 2, 1, 12, 2015, 31, 0.801    \ 2015, 2038.15, -0.5, 1385.90, 3, 1, 12, 2015, 31, 0.801"
local y_2016 "2016, 1100, 0.8, 880, 1, 1, 12, 2016, 31, 0.753  \ 2016, 1360.70, -0.3, 1088.56, 2, 1, 12, 2015, 31, 0.753    \ 2016, 2268.05, -0.5, 1542.24, 3, 1, 12, 2015, 31, 0.753"

mat sch_y = ( `y_2001' \ `y_2002' \ `y_2003' \ `y_2004' \ `y_2005' \ `y_2006' \ `y_2007' \ `y_2008' \ `y_2009' \ `y_2010' \ `y_2011' \ `y_2012' \ `y_2013' \ `y_2014' \ `y_2015' \ `y_2016' )
matlist sch_y


// Gathering relevant information for computing long term unemployment outcomes //
use C0_cash_did, replace

keep year id cpf dt_dism

rename year oyear // year of displacement
rename dt_dism odt_dism // original date of displacement
rename id oid // original id

preserve
forval year = 2005/2017 {
joinby cpf using "${prais}/`year'/1_`year's.dta"
cap g sbc=.
keep oyear oid odt_dism cpf identificad dt_hir dt_dism tempempr year causade mun nat ocup ind sbc salcont remmedia remmedr remdezr hor
save C0_additional`year' , replace
restore, pres
}
restore, not

// appending
clear
forval year = 2005/2017 {
append using C0_additional`year'
erase C0_additional`year'.dta
}

replace remmedr = remmedr*1.246 if year==2008
replace remmedr = remmedr*1.177 if year==2009
replace remmedr = remmedr*1.128 if year==2010
replace remmedr = remmedr*1.065 if year==2011
replace remmedr = remmedr*1 if year==2012
replace remmedr = remmedr*0.944 if year==2013
replace remmedr = remmedr*0.887 if year==2014
replace remmedr = remmedr*0.801 if year==2015
replace remmedr = remmedr*0.754 if year==2016
replace remmedr = remmedr*0.732 if year==2017

replace remdezr = remdezr*1.246 if year==2008
replace remdezr = remdezr*1.177 if year==2009
replace remdezr = remdezr*1.128 if year==2010
replace remdezr = remdezr*1.065 if year==2011
replace remdezr = remdezr*1 if year==2012
replace remdezr = remdezr*0.944 if year==2013
replace remdezr = remdezr*0.887 if year==2014
replace remdezr = remdezr*0.801 if year==2015
replace remdezr = remdezr*0.754 if year==2016
replace remdezr = remdezr*0.732 if year==2017

replace salc = salc*1.246 if year==2008
replace salc = salc*1.177 if year==2009
replace salc = salc*1.128 if year==2010
replace salc = salc*1.065 if year==2011
replace salc = salc*1 if year==2012
replace salc = salc*0.944 if year==2013
replace salc = salc*0.887 if year==2014
replace salc = salc*0.801 if year==2015
replace salc = salc*0.754 if year==2016
replace salc = salc*0.732 if year==2017


// remove duplicates
sort oyear oid cpf year identificad dt_hir remmedr ocup
by oyear oid cpf year identificad dt_hir remmedr ocup: g co = _n
keep if co==1
drop co

save C0_additionalbase , replace



// Creating variables //

use C0_additionalbase, clear

// time from last layoff allowing for ui eligibility
g prev_dtdism = dt_dism if dt_dism < odt_dism & tempempr*7/30 >= 6
gegen lastui = max(prev_dtdism) , by(oyear oid cpf)
drop prev_dtdism
label var lastui "Last Layoff allowing UI claim"

// Income y-1 y-2 y-3
g incpre0 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear
g incpre1 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear-1
g incpre2 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear-2

g incpos1 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear-1
g incpos2 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear-2
g incpos3 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear-3

foreach x in incpre0 incpre1 incpre2 incpos1 incpos2 incpos3 {
replace `x' = 0 if `x' == .
}

gegen preinc12 = total(incpos1 incpre0) , by(oyear oid cpf)
gegen preinc24 = total(incpos2 incpre1) , by(oyear oid cpf)
gegen preinc36 = total(incpos3 incpre2) , by(oyear oid cpf)

drop incpre0 incpre1 incpre2 incpos1 incpos2 incpos3

// Months y-1 y-2 y-3
g monpre0 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear
g monpre1 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear-1
g monpre2 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear-2

g monpos1 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear-1
g monpos2 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear-2
g monpos3 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear-3

foreach x in monpre0 monpre1 monpre2 monpos1 monpos2 monpos3 {
replace `x' = 0 if `x' == .
}

gegen premon12 = total(monpos1 monpre0) , by(oyear oid cpf)
gegen premon24 = total(monpos2 monpre1) , by(oyear oid cpf)
gegen premon36 = total(monpos3 monpre2) , by(oyear oid cpf)

drop monpre0 monpre1 monpre2 monpos1 monpos2 monpos3

drop if dt_hir < odt_dism // drop spells starting before displacement

sort oyear oid cpf dt_hir year
by oyear oid cpf: g n_und = (dt_hir - odt_dism)/7 if _n==1

// correcting dismissal date for years that do not contain day of dismissal
replace dt_dism = . if dt_dism < dt_hir

// Job Stability
gegen maxyear = max(year) , by(oyear oid cpf)
replace dt_dism = mdy(12,31,maxyear) if year==maxyear & dt_dism==. // Dismissal date censored at Dec 31st 2017
gegen firstdism = min(dt_dism), by(oyear oid cpf)
sort oyear oid cpf dt_hir year
by oyear oid cpf: g n_jdur = (firstdism - dt_hir)/7 if _n==1
drop maxyear firstdism

// Av. Job Stability
g jobdur = (dt_dism-dt_hir)/7 if causadesli!=0
replace jobdur = (mdy(month(odt_dism),day(odt_dism),year+3)-dt_hir)/7 if year==oyear+3
gegen jdur36 = mean(jobdur), by(oyear oid cpf)
gegen jobs36 = count(jobdur), by(oyear oid cpf)
drop jobdur

// Income y+1 y+2 y+3
g incpre1 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear+1
g incpre2 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear+2
g incpre3 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)*remmedr/30 , 0) if year==oyear+3

g incpos0 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear+0
g incpos1 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear+1
g incpos2 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)*remmedr/30 , 0) if year==oyear+2

foreach x in incpre1 incpre2 incpre3 incpos0 incpos1 incpos2 {
replace `x' = 0 if `x' == .
}

gegen inc12 = total(incpos0 incpre1) , by(oyear oid cpf)
gegen inc24 = total(incpos1 incpre2) , by(oyear oid cpf)
gegen inc36 = total(incpos2 incpre3) , by(oyear oid cpf)

drop incpre1 incpre2 incpre3 incpos0 incpos1 incpos2

// Months y+1 y+2 y+3
g monpre1 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear+1
g monpre2 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear+2
g monpre3 = max( (min(mdy(month(odt_dism),day(odt_dism),year),dt_dism)-max(mdy(1,1,year),dt_hir)+1)/30 , 0) if year==oyear+3

g monpos0 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear+0
g monpos1 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear+1
g monpos2 = max( (min(mdy(12,31,year),dt_dism)-max(dt_hir,mdy(month(odt_dism),day(odt_dism),year))+1)/30 , 0) if year==oyear+2

foreach x in monpre1 monpre2 monpre3 monpos0 monpos1 monpos2 {
replace `x' = 0 if `x' == .
}

gegen mon12 = total(monpos0 monpre1) , by(oyear oid cpf)
gegen mon24 = total(monpos1 monpre2) , by(oyear oid cpf)
gegen mon36 = total(monpos2 monpre3) , by(oyear oid cpf)

drop monpre1 monpre2 monpre3 monpos0 monpos1 monpos2

// keep single line per original spell
gsort oyear oid cpf dt_hir year
by oyear oid cpf: g co = _n
keep if co==1

rename mun n_mun
rename nat n_natjur
rename ocup n_ocup
rename ind n_ind
rename sbc n_sbc
rename remmedr n_remmedr
replace remdezr = . if dt_dism<mdy(12,31,year) // only if employed in december
rename remdezr n_remdezr
rename remmedia n_remmedia
rename salc n_salc
rename hor n_hor

keep oyear oid cpf lastui n_und n_jdur jdur36 jobs36 preinc12 preinc24 preinc36 premon12 premon24 premon36 inc12 inc24 inc36 mon12 mon24 mon36 n_mun n_natjur n_ocup n_ind n_sbc n_remmedr n_remdezr n_remmedia n_hor n_salc

rename oyear year
rename oid id

label var n_mun "Municipality"
label var n_natjur "Natjut"
label var n_remmedr "Earnigns"
label var n_remdezr "Dec earnings"
label var n_remdezr "Earnigns m.w."
label var n_salc "Contractual wage"
label var n_hor "Weekly hours"
label var n_ocup "Ocupation Code"
label var n_sbc "Firm sector"
label var n_ind "Industry"
label var n_und "Unemp. Duration"
label var n_jdur "Job Duration"
label var inc12 "Income year 1"
label var inc24 "Income year 2"
label var inc36 "Income year 3"
label var mon12 "Months worked year 1"
label var mon24 "Months worked year 2"
label var mon36 "Months worked year 3"
label var jdur36 "Av. job duration 3 years"
label var jobs36 "Number of jobs 3 years"

save C0_additional, replace

cap log close

*cap erase C0_additionalbase.dta




